package thread.dn;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo02 {

    private static int corePoolSize = 2;
    private static int maximumPoolSize = 3;
    private static long keepAliveTime = 1;
    private static TimeUnit unit = TimeUnit.SECONDS;
    /**
     * 工作队列
     */
    private static LinkedBlockingQueue<Runnable> workQueue =
            new LinkedBlockingQueue<>(1);
    /**
     * 线程工厂
     */
    private static ThreadFactory threadFactory = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r);
        }
    };

    /**线程池拒绝策略*/
    private static ThreadPoolExecutor.AbortPolicy handler =
            new ThreadPoolExecutor.AbortPolicy();//默认直接抛异常

    public static void main(String[] args) {
        //1.创建线程池
        ThreadPoolExecutor executor=
                new ThreadPoolExecutor(
                        corePoolSize,
                        maximumPoolSize,
                        keepAliveTime,
                        unit,
                        workQueue,
                        threadFactory,
                        handler);
        executor.execute(()->{
            System.out.println(Thread.currentThread().getName()+":task1");
        });
    }

}
